// Created by Xslt generator for Eclipse.
// XSL :  not found (java.io.FileNotFoundException:  (Bad file descriptor))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.ba360.struts.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import javax.naming.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ba360.portal.common.PortalConstants;
import com.ba360.portal.database.DBConnectionImpl;
import com.ba360.portal.database.dbo.User;
import com.ba360.portal.util.EmailSender;
import com.ba360.portal.util.Log;
import com.ba360.struts.form.RegistrationForm;

/**
 * RegistrationFormAction.java created by EasyStruts - XsltGen.
 * http://easystruts.sf.net created on 04-01-2004
 * 
 * XDoclet definition:
 * 
 * @struts:action path="/" name="RegistrationForm"
 *                input="/jsp/UserRegistration.jsp" validate="true"
 * @struts:action-forward name="/jsp/Login.jsp" path="/jsp/Login.jsp"
 * @struts:action-forward name="/jsp/UserRegistration.jsp"
 *                        path="/jsp/UserRegistration.jsp"
 */
public class RegistrationFormAction extends Action {

	// --------------------------------------------------------- Instance
	// Variables

	// --------------------------------------------------------- Methods

	/**
	 * Method execute
	 * 
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RegistrationForm registrationForm = (RegistrationForm) form;
		Log.init(this.getClass());
		if (registrationForm == null) {
			return mapping.findForward("faliure");
		}
		String value = "";
		String email = registrationForm.getEmail();		
		String city = registrationForm.getCity();
		String password = registrationForm.getPassword();
	
		Context context = null;
		DataSource ds = null;
		Connection conn = null;
		Statement stmt = null;
		DBConnectionImpl dbConn = new DBConnectionImpl();
		conn = dbConn.getConnection();
		String action = request.getParameter(PortalConstants.ACTION);
		HttpSession session = request.getSession();
		if (action == null) {
			return mapping.findForward("failure");
		}
		//throw new UnsupportedOperationException("Generated method
		// 'execute(...)' not implemented.");
		if (action.equals("Save")) 
		{			
			session.removeAttribute("NEWUSER");
				try 
				{
					stmt = conn.createStatement();				
					ResultSet rs = stmt.executeQuery("select count(*) from user_profile where email='"+ registrationForm.getEmail() + "'");
					rs.next();
					int count = Integer.parseInt(rs.getString(1));
					session.removeAttribute("USER_EXIST");
					Log.info("User Exist: " + count);
						if (count > 0) 
						{
							Log.info("Found User Match " + registrationForm.getEmail());
							session.setAttribute("USER_EXIST", registrationForm.getEmail());
							return mapping.findForward("failure");
						}
							String insStatement = "INSERT INTO user_profile (email,city,password) VALUES(";						
							value = "'" + email + "','" + city + "','" + password+ "')";
							Log.info(insStatement + value);
							stmt.execute(insStatement + value);							
							session.setAttribute("NEWUSER", "NEWUSER");
							Log.info(" Registration successfull for user: "+ registrationForm.getEmail()+"\n");
							Log.info(" Registration successfull for password: "+ registrationForm.getPassword()+"\n");
							
							Properties prop = new Properties();
							prop.setProperty("EmailAddr",registrationForm.getEmail() );
							prop.setProperty("Password", registrationForm.getPassword());
							try
							{
								EmailSender.sendEmail(EmailSender.TYPE_REGISTRATION,prop);
							}
							catch (Exception e)
							{								
								Log.error(e.getMessage());
							}				
							Log.info(" Send email to: "+ registrationForm.getEmail());
							conn.close();
				}
				catch (Exception e) 
				{
					Log.error(e.getMessage());
					e.printStackTrace();
					return mapping.findForward("failure");
			    }
			finally
			{
				conn.close();
			}
			return mapping.findForward("success");

		} 
		else if (action.equals("UpdateInfo")) {
			try {

				User user = (User) session.getAttribute(PortalConstants.USER);
				System.out.println("it comes inside update info");				
				String updateStmt = "update user_profile set ";
				value=User.USER_EMAIL + "='" + email + "'," +					  
					  User.USER_CITY + "='" + city + "'," +
					  User.USER_PASS + "='" + password + "'";				
				String whereClause = " where userid=" + user.getUserId();
				stmt = conn.createStatement();
				Log.info(updateStmt + value + whereClause);
				stmt.executeUpdate(updateStmt + value + whereClause);
				stmt.close();
				user = new LoginAction().populateUserInfo(conn, user.getUserId(), registrationForm.getPassword());
				session.setAttribute(PortalConstants.USER, user);
				return mapping.findForward("portal");
			} catch (Exception e) {
				Log.error(e.getMessage());
				return mapping.findForward("failure");
			}

		} else if (action.equals("Create")) {
			session.removeAttribute("USER_EXIST");
			return mapping.findForward("failure");
		}else if (action.equals("ViewInfo"))
		{
			User user = (User)session.getAttribute("USER");		
			return mapping.findForward("failure");
		}

		return mapping.findForward("login");
	}

}